const app = getApp();
const utils = require('../../utils/utils.js');
let PAGE = {page: 1, page_size: 10};
Page({
    data: {
        //订单总数量
        total: 0,
        isClick: true,
        //订单状态
        status: 1,
        //订单数据
        list: []
    },
    onLoad: function (options) {
        wx.showLoading({
            title: 'loading...',
            mask: true
        });
        PAGE = {page: 1, page_size: 10};
        // 获取用户使用记录信息
        this.getUsedList();
        wx.hideLoading();
    },
    onShow: function () {
        wx.showLoading({
            title: 'loading...',
            mask: true
        });
        PAGE = {page: 1, page_size: 10};
        this.getUsedList();
        wx.hideLoading();
    },
    // 复制订单号
    handleCopy: function (e) {
        let usedsn = e.currentTarget.dataset.usedsn;
        wx.setClipboardData({
            data: usedsn,
            success(res) {
                wx.showToast({
                    title: "复制成功",
                    icon: 'none',
                    duration: 2000
                });
            }
        });
    },
    // ================= 获取用户使用记录信息 ========================
    getUsedList() {
        let that = this;
        let newList = [];
        let oldList = that.data.list;
        utils.httpRequest('/usedList', {
            page_size: PAGE.page_size,
            page: PAGE.page
        }, function (res) {
            if (res.code == 1) {
                that.setData({
                    total: res.data.total
                })
                if (PAGE.page > 1) {
                    newList = res.data.list;
                    that.setData({
                        list: oldList.concat(newList)
                    })
                } else {
                    that.setData({
                        list: res.data.list
                    })
                }
            }
        })
    },
    // ========================= 刷新下一页 ====================
    onReachBottom() {
        let that = this;
        if (that.data.total > that.data.list.length) {
            PAGE.page++;
            that.getUsedList();
        } else {
            return
        }
    },
    // ========================= 跳转到使用记录详情页面 ======================
    goToDetail(e) {
        let that = this;
        let usedId = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../usedDetails/usedDetails?used_id=' + usedId
        });
    },
});